<template>
  <div class="change-approval">
    <el-table
      v-loading="listLoading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="标题" prop="title" show-overflow-tooltip />
      <el-table-column
        label="变更类型"
        align="center"
        prop="changeTypeName"
        width="120"
      />
      <el-table-column
        label="变更原因"
        prop="changeReason"
        show-overflow-tooltip
      />
      <el-table-column
        label="变更前"
        prop="changeBefore"
        show-overflow-tooltip
      />
      <el-table-column
        label="变更后"
        prop="changeAfter"
        show-overflow-tooltip
      />
      <el-table-column
        label="申请人"
        align="center"
        prop="updateBy"
        width="120"
      />
      <el-table-column
        label="申请时间"
        align="center"
        prop="updateTime"
        width="170"
      />
      <el-table-column label="审批结果" align="center" width="120">
        <template slot-scope="{ row }">
          {{ renderResultStatus(row.approveResult) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.approveResult === null"
            size="mini"
            type="primary"
            plain
            @click="view(row)"
          >
            审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px" />
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="pageNum"
      :page-size="pageSize"
      :total="total"
      @size-change="onPageSizeChange"
      @current-change="onCurrentPageChange"
    />

    <el-dialog
      width="640px"
      title="项目变更"
      :visible.sync="visible"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form ref="form" :model="form" label-width="5em">
        <el-form-item label="标题">
          <el-input v-model="form.title" disabled />
        </el-form-item>
        <el-form-item label="变更类型">
          <el-select v-model="form.changeType" disabled>
            <el-option
              v-for="(item, index) in changeTypeList"
              :key="index"
              :value="item.key"
              :label="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="变更原因">
          <el-input
            type="textarea"
            v-model="form.changeReason"
            :autosize="{ minRows: 2, maxRows: 5 }"
            disabled
          />
        </el-form-item>
        <el-form-item label="变更前">
          <el-input
            type="textarea"
            v-model="form.changeBefore"
            :autosize="{ minRows: 2, maxRows: 5 }"
            disabled
          />
        </el-form-item>
        <el-form-item label="变更后">
          <el-input
            type="textarea"
            v-model="form.changeAfter"
            :autosize="{ minRows: 2, maxRows: 5 }"
            disabled
          />
        </el-form-item>
        <el-form-item class="dialog-footer">
          <el-button @click="visible = false">关 闭</el-button>
          <el-button type="danger" @click="examine(1)">驳 回</el-button>
          <el-button type="primary" @click="examine(0)">通 过</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getChangePage,
  typeList,
  getDetails,
  approveChange,
} from "@/api2/projectManagement/process";

export default {
  inject: ["projectId"],
  data() {
    return {
      tableData: [],
      listLoading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      visible: false,
      form: {},
      changeTypeList: [],
    };
  },
  mounted() {
    this.getOption();
    this.getList();
  },
  methods: {
    renderResultStatus(status) {
      const obj = {
        0: "已通过",
        1: "已驳回",
      };
      return obj[status];
    },
    async getOption() {
      const { data } = await typeList();
      this.changeTypeList = data;
    },
    getList() {
      this.listLoading = true;
      getChangePage({
        projectId: this.projectId,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then((data) => {
        this.listLoading = false;
        this.tableData = data.data;
        this.total = data.total;
      });
    },
    onPageSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    onCurrentPageChange(val) {
      this.pageNum = val;
      this.getList();
    },
    async view({ changeId }) {
      this.visible = true;
      const res = await getDetails({ changeId });
      this.form = res.data;
    },
    async examine(type) {
      const { changeId } = this.form;
      const typename = type ? "驳回" : "通过";
      await approveChange({ changeId, approveResult: type });
      this.$message.success("申请已" + typename);
      this.visible = false;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  margin-bottom: 0;
  text-align: center;
  ::v-deep {
    .el-form-item__content {
      margin-left: 0 !important;
    }
  }
}
</style>
